<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: DataLang.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");
Zend_Loader::loadClass("MultilangInfo");
class DataLang extends Baogg_Db_Table {
	protected $_name,$tableId,$languageId,$pk,$langInfo,$allowSameContent,$sameCondition;
	
	function __construct($table_name,$language_id='',$primary_key='',$allowSameContent=false,$sameCondition='') {
		global $config,$db;
		$g_admin_language_id = $config['lang']['default'];

		$this->langInfo=new MultilangInfo();
		$this->_name=$table_name;
		$this->tableId=$this->langInfo->getTableIdByName($this->_name);
		if(!$this->tableId){
			throw new Exception("DataLang Construct Error:invalid table name.");
		}
		$this->languageId=$language_id?$language_id:$g_admin_language_id;
		$this->pk=$primary_key?$primary_key:$table_name."_id";		
		$this->db = $db;
		
		$this->allowSameContent=$allowSameContent;
		$this->sameCondition=$sameCondition?(array)$sameCondition:array();
	}
	function getParams(){
		return function_exists("getUrlParam")?getUrlParams():$_REQUEST;
	}
	function getDataLang($where='', $order='',& $pager=''){
		return $this->langInfo->getDataLang($this->_name,$where,$order,$this->languageId,& $pager,$this->pk);
	}

	function getList($where='', $order=''){
		$rows=$this->getDataLang($where,$order);
		$pk=$this->pk;
		foreach ( ( array ) $rows  as $key => $value ) {
			$module=getModuleName();
			$controller=getControllerName();
			$action = editActionLink ( "{$config['web']['baseurl']}{$module}/{$controller}/edit/$pk/{$value[$pk]}/multilang_table_id/{$value['multilang_table_id']}" );
			$action .= deleteActionLink ( "{$config['web']['baseurl']}{$module}/{$controller}/delete/$pk/{$value[$pk]}/multilang_table_id/{$value['multilang_table_id']}" );
			$list [$key] = $value;
			$list [$key] ['action'] = $action;
			$list [$key] ['state'] = publishActionLink ( $key, $value ['state'], $this->_name, $pk, $value[$pk] );
		}
		return $list;
	}

	function insert(& $arr=array()) {
		$arr=$arr?$arr:$this->getParams();
		//$arr=array_map("stripslashes",$arr);
		//$arr['key_id']=$arr['key_id']?$arr['key_id']:$arr[$this->pk];
		$arr['language_id']=$arr['language_id']?$arr['language_id']:$this->languageId;
		$arr['multilang_table_id']=$arr['multilang_table_id']?$arr['multilang_table_id']:$this->tableId;
		
		$this->sameCondition[]=$this->db->quoteInto("b.content=?",$arr['content']);
		/*var_dump(!$this->checkSameContent($arr));
			echo debugDb();
			exit;*/
		if(!$this->checkSameContent($arr)){
			
			return -1;
		}
		$_arr = filterColumn ( $this->_name, $arr );
		unset($_arr[$this->pk]);
		$rows_affected = $this->db->insert ( $this->_name, $_arr );
		if ($rows_affected) {
			$lastId = $this->db->lastInsertId ();
		}

		$arr['key_id']=$lastId;
		if($lastId){
			$rows_affected = $this->langInfo->insert($arr );
		}
		$this->sameCondition=array();
		return $rows_affected;
	}

	function update($arr=array()){
		$arr=$arr?$arr:$this->getParams();
		//$arr=array_map("stripslashes",$arr);
		$arr['key_id']=$arr['key_id']?$arr['key_id']:$arr[$this->pk];
		$arr['language_id']=$arr['language_id']?$arr['language_id']:$this->languageId;
		$arr['multilang_table_id']=$arr['multilang_table_id']?$arr['multilang_table_id']:$this->tableId;

		if (! $arr [$this->pk]) {
			return null;
		}
		
		$this->sameCondition[]="a.{$this->pk} != '{$arr['key_id']}'";
		$this->sameCondition[]=$this->db->quoteInto("b.content=?",$arr['content']);
		if(!$this->checkSameContent($arr)){
			return -1;
		}
		
		$_arr = filterColumn ( $this->_name, $arr );
		$where = $this->db->quoteInto ( "{$this->pk} = ?", $_arr [$this->pk] );
		unset ( $_arr [$this->pk] );
		try{
			$rows_affected = $this->db->update ( $this->_name, $_arr, $where );
			$content=$this->langInfo->getInfoName($arr);			
			if(isset($content)){
				$rows_affected2 = $this->langInfo->update( $arr );
			}else{
				$rows_affected2 =  $this->langInfo->insert($arr);
			}
			$this->sameCondition=array();
		}catch(Exception $e){
			return 0;
		}		
		return 1;
	}

	function delete($arr=array()){
		$arr=$arr?$arr:$this->getParams();
		if(!$arr[$this->pk]){
			throw new Exception("Please give an key id.");
			exit;
		}
		$arr['key_id']=$arr['key_id']?$arr['key_id']:$arr[$this->pk];
		$arr['language_id']=$arr['language_id']?$arr['language_id']:$this->languageId;
		$arr['multilang_table_id']=$arr['multilang_table_id']?$arr['multilang_table_id']:$this->tableId;

		if(!is_array($arr) || !$arr[$this->pk]){
			return null;
		}

		$ids=(array)$arr[$this->pk];

		$dbFlag=1;
		foreach($ids as $id){
			$where = $this->db->quoteInto ( "{$this->pk} = ?", $id );
			$rows_affected = $this->db->delete ( $this->_name, $where );
			$dbFlag=$dbFlag && $rows_affected ;
		}

		$dbFlag2=$this->langInfo->delete($arr);
		return $dbFlag && $dbFlag2;
	}

	function setLangId($langId){
		if(is_numeric($langId)){
			$this->languageId=$langId;
		}
	}

	function getInfoName($arr=array()) {
		$arr=$arr?$arr:$this->getParams();
		$arr['key_id']=$arr['key_id']?$arr['key_id']:$arr[$this->pk];
		$arr['language_id']=$arr['language_id']?$arr['language_id']:$this->languageId;
		$arr['multilang_table_id']=$arr['multilang_table_id']?$arr['multilang_table_id']:$this->tableId;

		return $this->langInfo->getInfoName($arr);
	}

	function getTableIdByName($name=''){
		if($name){
			$retTableId = $this->langInfo->getTableIdByName($name);
		}else{
			$retTableId = $this->tableId;
		}
		return $retTableId;
	}

	function getSelects($result=array()) {
		$parentKey=$this->_name."_pid";
		return $this->langInfo->getSelects($result,$this->pk,$parentKey);
	}

	function getModulePic(){
		global $config;
		//image files
		$dir = $config['system']['root']."html/theme/backend/".$config['style']['folder']."/images/module/";
		/*echo $config['system']['root'];
		exit;*/
		foreach((array)glob($dir.'{*.gif,*.jpg,*.png}', GLOB_BRACE) as $imageFiles)
		{
			$file = preg_replace('/[\s\S]*\/(.[^\/]+)$/i','$1',$imageFiles);
			//$images[$file]  = substr($file,0,strpos($file,'.'));
			$images[$file]  = $file;
		}
		/*echo $dir;
		var_dump($images);
		exit;*/
		return $images;
	}
	function getLanguageName($languageId=''){

		//get language name
		Zend_loader::loadClass("Language");
		$languageId =(int)$languageId>0?$languageId:$this->languageId;
		$language=new Language();
		$langList=$language->getOne($languageId);
		$langName=$langList[0]['language_name'];
		return $langName;
	}
	function getLanguage($languageId=''){

		//get language name
		Zend_loader::loadClass("Language");
		$languageId =(int)$languageId>0?$languageId:$this->languageId;
		$language=new Language();
		$langList=$language->getOne($languageId);
		$langName=$langList[0];
		return $langName;
	}
	function getLanguageCode($languageId=''){

		//get language name
		Zend_loader::loadClass("Language");
		$languageId =(int)$languageId>0?$languageId:$this->languageId;
		$language=new Language();
		$langList=$language->getOne($languageId);
		$langName=$langList[0]['language_code'];
		return $langName;
	}
	function checkExistContent($arr=array()){
		if(!$arr['table_name']){
			$arr['table_name']=$this->_name;
		}
		
		return $this->getDataLang($this->sameCondition);
		
		//return $this->langInfo->checkExistContent($arr,$this->sameCondition);
	}
	function setSameCondition($sameCondition){
		$this->sameCondition=(array)$sameCondition;
	}
	function checkSameContent($arr){
				
		if(!$this->allowSameContent && $this->checkExistContent($arr)){
			return false;
		}else{
			return true;
		}
	}
}
